package com.atguigu.yygh.orders.service.impl;

import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.atguigu.yygh.orders.mapper.RefundInfoMapper;
import com.atguigu.yygh.orders.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 退款信息表 服务实现类
 * </p>
 *
 * @author 卓抱抱
 * @since 2022-03-20
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    //创建一个退款的订单
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {

        // 保存前，我们需要进行查询是否存在相同数据
        QueryWrapper<RefundInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",paymentInfo.getOrderId());
        RefundInfo refundInfo = baseMapper.selectOne(wrapper);

        if (!StringUtils.isEmpty(refundInfo)){
            return refundInfo;
        }

        refundInfo = new RefundInfo();
        refundInfo.setCallbackContent(paymentInfo.getCallbackContent());
        refundInfo.setCallbackTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        //退款中
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        refundInfo.setUpdateTime(new Date());
        refundInfo.setCreateTime(new Date());
        refundInfo.setTradeNo(paymentInfo.getTradeNo());


        // 然后进行保存
        baseMapper.insert(refundInfo);
        return refundInfo;
    }


}
